package com.bootdo.act.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.act.dao.ActDetailsDao;
import com.bootdo.act.dao.ActTypeDao;
import com.bootdo.act.domain.ActDetailsDO;
import com.bootdo.act.domain.ActTypeDO;
import com.bootdo.act.service.ActDetailsService;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.system.domain.DeptDO;



@Service
public class ActDetailsServiceImpl implements ActDetailsService {
	@Autowired
	private ActDetailsDao actDetailsDao;
	@Autowired
	private ActTypeDao actTypeMapper;
	
	@Override
	public ActDetailsDO get(Long id){
		return actDetailsDao.get(id);
	}
	
	@Override
	public List<ActDetailsDO> list(Map<String, Object> map){
		return actDetailsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return actDetailsDao.count(map);
	}
	
	@Override
	public int save(ActDetailsDO actDetails){
		return actDetailsDao.save(actDetails);
	}
	
	@Override
	public int update(ActDetailsDO actDetails){
		return actDetailsDao.update(actDetails);
	}
	
	@Override
	public int remove(Long id){
		return actDetailsDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return actDetailsDao.batchRemove(ids);
	}

	@Override
	public Tree<ActDetailsDO> getTree() {
		List<Tree<ActDetailsDO>> trees = new ArrayList<Tree<ActDetailsDO>>();
		List<ActTypeDO> types = actTypeMapper.list(new HashMap<String, Object>(16));
		Long[] pTypes = actTypeMapper.listParentTypes();
		Long[] dTypes = actDetailsDao.listAllType();
		Long[] allTypes = (Long[]) ArrayUtils.addAll(pTypes, dTypes);
		for (ActTypeDO type : types){
			if (!ArrayUtils.contains(allTypes, type.getId())) {
				//continue;
			}
			Tree<ActDetailsDO> tree = new Tree<ActDetailsDO>();
			tree.setId(type.getId().toString());
			tree.setParentId(type.getParentId().toString());
			tree.setText(type.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", false);
			state.put("mType", "type");
			tree.setState(state);
			trees.add(tree);
		}
		HashMap<String, Object> map = new HashMap<String, Object>(16);
		map.put("status", 1);
		List<ActDetailsDO> actDetails = actDetailsDao.list(map);
		for (ActDetailsDO actDetail : actDetails) {
			Tree<ActDetailsDO> tree = new Tree<ActDetailsDO>();
			tree.setId(actDetail.getId().toString());
			tree.setParentId(actDetail.getTypeId().toString());
			tree.setText(actDetail.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			state.put("mType", "actDetail");
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ActDetailsDO> t = BuildTree.build(trees);
		/*Tree<ActDetailsDO> root = new Tree<ActDetailsDO>();
		root.setId("-1");
		root.setParentId("");
		root.setHasParent(false);
		root.setChildren(true);
		root.setChecked(true);
		root.setChildren(trees);
		root.setText("有效活动");
		Map<String, Object> state = new HashMap<>(16);
		state.put("opened", true);
		root.setState(state);*/
		
		return t;
	}
	
}
